<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$startt = (float) array_sum(explode(' ', microtime()));

$hostname = 'localhost';
$username = 'root';
$password = 'root';

$db = null;
try {
    $db = new PDO("mysql:host=$hostname;dbname=wikipedia", $username, $password);
    echo 'Connected to database';

    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

    $sql = "Select * from articles LIMIT 15";
    $result = $db->query($sql);
    $i = 1;

    $fp = fopen("/home/neozilon/wikipedia/importar.txt", "w");
    foreach ($result as $row) {
        $article = utf8_encode($row["content"]);
        $title = utf8_encode($row["title"]);
        # Get rid of newlines in title
        $title = str_replace("\n", "", $title);
        # Make Safetitle for URL
        $safetitle = str_replace(" ", "_", $title);

        //now get the content
        $content = null;
        try {
            $content = file_get_contents('http://localhost/mediawiki/index.php?title=' . $safetitle . '&action=render');
        } catch (Exception $e) {
            
        }

        if ($content != null) {
            $insert = "Insert into parser (title,content) values(?,?)";
            $stm = $db->prepare($insert);

                $content = mb_convert_encoding($content,"ISO-8859-1");
                $title = mb_convert_encoding($title,"ISO-8859-1");

            //    echo "title: " . $title . '\n';
            //    echo "content: " . $content . '\n';

            $stm->bindParam(1, $title);
            $stm->bindParam(2, $content);
            $stm->execute();
        }
    }
    fclose($fp);
} catch (PDOException $e) {
    echo $e->getMessage();
    echo "ERROR";
    exit();
}

$endt = (float) array_sum(explode(' ', microtime()));
echo "\n ***************************** \n";
echo "Processing time: " . sprintf("%.4f", ($endt - $startt)) . " seconds";
echo "\n ***************************** \n";
?>
